package com.ngkj.zmfptback.sys.initData.controller;

import com.ngkj.zmfptCommon.common.bean.CommonReslut;
import com.ngkj.zmfptCommon.common.util.ResultEnum;
import com.ngkj.zmfptback.sys.initData.bean.SysArea;
import com.ngkj.zmfptback.sys.initData.servicie.read.AreaService;
import com.ngkj.zmfptback.sys.initData.servicie.write.AreaWriteService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @author ChenHN
 * @since 2021/11/1 creation
 **/
@RestController
public class AreaContoller {

    @Resource
    private AreaService areaService;

    @Resource
    private AreaWriteService areaWriteService;

    /**
     * 根据pid 获取到下一级的所有城市
     *
     * @param areaId
     * @return
     */
    @GetMapping("/getArea")
    public CommonReslut<List<SysArea>> getArea(String areaId) {
        List<SysArea> sysAreaList = null;
        sysAreaList = areaService.getSelectAreas(areaId);
        return new CommonReslut<>(ResultEnum.ok, sysAreaList);
    }

    @GetMapping("/area/getAreaTree")
    public CommonReslut getAreaTree(SysArea sysArea) {
        try {
            List<Map<String, Object>> allAreas2Tree = areaService.getAllAreas2Tree(sysArea);
            return new CommonReslut(ResultEnum.ok, allAreas2Tree);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    /**
     * 以城市首字母分组，返回城市列表
     *
     * @return
     */
    @GetMapping("/getAreaGroupFirst")
    public CommonReslut<Map<String, List<SysArea>>> getAreaGroupFirst() {
        Map<String, List<SysArea>> groupFirstFromCache = areaService.getGroupFirstFromCache();
        return new CommonReslut<>(ResultEnum.ok, groupFirstFromCache);
    }

    /**
     * 根据id获取，该区域信息
     *
     * @return
     */
    @GetMapping("/area/getAreaById")
    public CommonReslut<Map<String, Object>> getAreaById(String id) {
        try {
            Map<String, Object> areaById = areaService.getAreaById(id);
            return new CommonReslut<Map<String, Object>>(ResultEnum.ok, areaById);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut<Map<String, Object>>(ResultEnum.error);
        }
    }

    @DeleteMapping("/area/deleteArea")
    public CommonReslut deleteArea(@RequestBody  SysArea sysArea) {
        areaWriteService.deleteArea(sysArea.getAreaId());
        return new CommonReslut(ResultEnum.ok);

    }

    @PostMapping("/area/addArea")
    public CommonReslut addArea(@RequestBody SysArea sysArea) {
        try {
            areaWriteService.addArea(sysArea);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @PostMapping("/area/editArea")
    public CommonReslut editArea(@RequestBody SysArea sysArea) {
        try {
            areaWriteService.editArea(sysArea);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            return new CommonReslut(ResultEnum.error);
        }
    }


}
